
#include ".\consolewnd.h"

CConsoleWnd::CConsoleWnd(void)
{
	m_hStdConsole = INVALID_HANDLE_VALUE;
#ifdef _DEBUG
	if (!AllocConsole())
	{
		m_hStdConsole = INVALID_HANDLE_VALUE;
	}

	m_hStdConsole = GetStdHandle(STD_OUTPUT_HANDLE);
#endif	
}

CConsoleWnd::~CConsoleWnd(void)
{
	if (m_hStdConsole != INVALID_HANDLE_VALUE)
	{
		FreeConsole();
	}
}



void CConsoleWnd::PrintLog(LPCSTR szLogString /* = "" */)
{
	if (m_hStdConsole == INVALID_HANDLE_VALUE)
	{
		return;
	}
	if (szLogString == NULL)
	{
		return;
	}
	if (strlen(szLogString) <= 0)
	{
		return;
	}
	DWORD nLength = 0;
	WriteConsole(m_hStdConsole,szLogString,strlen(szLogString),&nLength,NULL);
	WriteConsole(m_hStdConsole,"\n",strlen("\n"),&nLength,NULL);
	return;
}

void CConsoleWnd::PrintFormatLog(LPCSTR szFormat,...)
{
	TCHAR szBuffer[1024] = {0};
	va_list pArgList;

	// The va_start macro (defined in STDARG.H) is usually equivalent to:
	// pArgList = (char *) &szFormat + sizeof (szFormat) ;

	va_start(pArgList,szFormat);

	// The last argument to wvsprintf points to the arguments

	_vsntprintf(szBuffer,sizeof(szBuffer)/sizeof(TCHAR),szFormat,pArgList);

	// The va_end macro just zeroes out pArgList for no good reason

	va_end(pArgList) ;

	PrintLog(szBuffer);
}

void CConsoleWnd::DiableCloseButton()
{
	if (m_hStdConsole == INVALID_HANDLE_VALUE)
	{
		return;
	}
	char szTittle[255] = {0};
	GetConsoleTitle(szTittle,255);
	HWND hWnd = FindWindow("ConsoleWindowClass",szTittle);
	HMENU hMenu = NULL;
	hMenu = GetSystemMenu(hWnd,FALSE);
	RemoveMenu(hMenu,SC_CLOSE,MF_BYCOMMAND);
}

void CConsoleWnd::EnableColseButton()
{

}